<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use Curl\Curl;
use think\Cache;
use think\Config;
use think\Db;
use think\Request;

/**
 * 企业
 * Class Industry
 * @package app\index\controller
 */
class Company extends BaseController
{
    /**
     * 企业列表
     */
    public function companyList(){
        $reqData['industry_id'] = $this->request->get('industry_id');
        $reqData['city_id'] = $this->request->get('city_id');
        $reqData['chain_id'] = $this->request->get('chain_id');
        $tank_id = $this->request->get('tank_id');
        $keywords = $this->request->get('keywords','');
        $top = $this->request->get('top');
        $method = $this->request->method();
        $page = $this->request->get('page',1);
        $sortBy= $this->request->get('sortBy','score_1');
        $city_name= $this->request->get('city_name','');
        $page=$page>=250 ? 250 : $page;
        $limit = $this->request->get('limit',10);
        $tank_id = isset($tank_id) && !empty($tank_id) ? 1 : 0;
        $dispatch = $this->request->dispatch();

        // 业务
        if ($reqData['city_id']==1){
            $reqData['city_id']=0;
        }
        $request_arr = ['sortBy'=>$sortBy,'top'=>$top,'chain_id'=>$reqData['chain_id'],'industry_id' => $reqData['industry_id'],'city_id' => $reqData['city_id'],'tank_id'=>$tank_id, 'keywords'=>$keywords,'page'=>$page,'limit'=>$limit ];
        $curl_data = $this->getBusiness('Common')->curl_request_data($method,$request_arr);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        foreach ( $curl_data['data']['data']['data'] as &$value){
            $value['chain_name']=empty($value['chain_name']) ? '暂无' :$value['chain_name'];
            if ($value['city_str']){
                $value['city_str']=str_replace("'",'"', $value['city_str']);
                $value['city_str']=array_column(json_decode($value['city_str'],true),'name');
                $value['city_name']=isset($value['city_str'][1]) ? $value['city_str'][1] : '';
                unset( $value['city_str']);
            }
            if ($value['industry_str']){
                $value['industry_str']=str_replace("'",'"', $value['industry_str']);
                $value['industry_name']=implode(',',array_column(json_decode($value['industry_str'],true),'name'));
                unset($value['industry_str']);
            }
        }
        // 我的收藏
        $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$curl_data['data']['data']['data'],Config::get('collect.2'));
        $curl_data['data']['data']['data'] = $return_data;
        // 一亿三数据
        if (empty($reqData['industry_id'])&&empty($reqData['city_id'])&&empty($reqData['chain_id'])&&empty($keywords)&&empty($top)){
            $curl_data['data']['data']['total_1']= $curl_data['data']['data']['total']*2300;
        }else{
            $curl_data['data']['data']['total_1']=$curl_data['data']['data']['total'];
        }
        $curl_data['data']['data']['total']=$curl_data['data']['data']['total']>=3750 ? 3750 :$curl_data['data']['data']['total'];

        $this->result['status'] = 1;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 企业详情
     * @return false|string
     */
    public function companyDetail()
    {
        $reqData['identification'] = $this->request->get('identification');
        $reqData['method'] = $this->request->method();
        $dispatch = $this->request->dispatch();

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('identification' => $reqData['identification']));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        // 我的收藏字段
        $single = array($curl_data['data']['data']);
        $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$single,Config::get('collect.2'));
        $curl_data['data']['data'] = $return_data;

        //是否监控
        $flag = db('project')->where(['status' => 1, 'identification' => $reqData['identification']])->value('is_monitor');
        $curl_data['data']['data'][0]['have']=empty($flag) ? 2 : $flag;


        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 企业评论列表
     */
    public function companyCommentList()
    {
        $reqData['identification'] = $this->request->get('identification');
        $page = $this->request->get('page',0);
        $limit = $this->request->get('limit',10);

        if (!$reqData['identification']){
            $this->result['status'] = -1;
            $this->result['msg'] = '参数错误';
            return json_encode($this->result);
        }
        $result = $this->getBusiness('Company')->companyCommentList($reqData['identification'],$page,$limit);
        return json_encode($result);
    }

    /**
     * 企业添加评论
     */
    public function companyCommentAdd()
    {
        $reqData['identification'] = $this->request->post('identification');
        $reqData['star_num'] = $this->request->post('star_num');
        $reqData['content'] = $this->request->post('content');
        $uuid = $this->request->dispatch()['user_no'];
        $tokenPassword = $this->request->header('tokenPassword');
        if(!$reqData['star_num']) $reqData['star_num'] = 3;

        // 表单验证
        $validata = $this->validate($reqData,'app\index\validate\CompanyCommentAdd');

        if (true !== $validata) {
            // 验证失败 输出错误信息
            $this->result['status'] = -1;
            $this->result['msg'] = $validata;
            return json_encode($this->result);
        }
        $result = $this->getBusiness('Company')->companyCommentAdd($reqData,$uuid,$tokenPassword);
        return json_encode($result);

    }

    /**
     * 推荐企业
     */
    public function pushCompany()
    {
        $obj = db('push_company')->where('status', 1);
        $dispatch = $this->request->dispatch();
        if (!isset($dispatch['user_no'])) {
            $dispatch['user_no'] = 0;
        }
        $result = $obj->where('user_no', $dispatch['user_no'])
            ->field('*', 'id')
            ->order('id', 'desc')
            ->select()
            ->toArray();
        if (empty($result)){
            $result = $obj->where('user_no', 0)
                ->field('*', 'id')
                ->order('id', 'desc')
                ->select()
                ->toArray();
        }
        foreach ($result as &$value) {
            if (!empty($value['industry'])){
                $value['industry']=implode(',',array_column(unserialize($value['industry']),'name'));
            }
        }
        return json_encode(array('status' => 1, 'msg' => 'success', 'data' => $result));
    }
}